/**
 * 自用业务模块
 * @type {me|exports|module.exports}
 */
const Rid = require('uuid');
const SHA1 = require('crypto-js/sha1');
const superagent = require('superagent');

var headers = {
    Host: "bim5d.glodon.com",
    Accept: "application/json, text/javascript",
    Origin:" http://bim5d.glodon.com",
    "X-Requested-With": "XMLHttpRequest",
    'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.78 Safari/537.36',
    'Content-Type': 'application/x-www-form-urlencoded',
    Referer: 'http://bim5d.glodon.com/api/login-comn.jsp',
    'Accept-Encoding': 'gzip, deflate',
    'Accept-Language': 'zh-CN,zh;q=0.8,zh-TW;q=0.6,en;q=0.4,sr;q=0.2'
};


function login(callback){
    superagent.post('http://bim5d.glodon.com/api/v1/oauth2/checkUser')
        .set(headers)
        .send({username:"867739285@qq.com",password:"123abc",grant_type:"password"})                                                                                       //这肯定不是我真的用户名和密码啦
        .end(function(err, res){
            callback(res)
        })
}

function check(res,call){

    var data=JSON.parse(res.text);
    var token=data.token;
    console.log(data);

    superagent.get('http://bim5d.glodon.com/api/v1/notification/statistic?token='+token)
        .set(headers)
        .end(function(err, res){
            var cookie=res.header['set-cookie'];
            call(cookie);
        })



}

module.exports = [{
    method: 'GET',
    path: '/proxy/login',
    handler: function (req, rp) {
        login(function(res){
            check(res,function(cookie){
                rp(cookie);
            })
        })
    }
}]

